home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Skunkware 5
/
Skunkware 5.iso
/
src
/
Games
/
connx-1.0
/
connect.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-06-25
|
3KB
|
165 lines
/*
* Used by server to play the game.
*/
#include "connect.h"
/* globals */
int the_game, num_pieces, num_rows, num_columns, num_players, num_to_connect;
board conn_board;
/* prototpyes */
int board_piece (int row, int column);
/**************************************************************************/
/* MAIN ROUTINES **********************************************************/
/**************************************************************************/
/*
* start a new game
*/
void
allocate (int game_type, int rows, int columns, int players, int conn)
{
num_rows = rows;
num_columns = columns;
bzero (conn_board, sizeof (board));
num_players = players;
num_to_connect = conn;
num_pieces = 0;
the_game = game_type;
}
/***************************************************************************/
/*
* called for each players move
*/
int
add_to_board (coord * move, int player_no)
{
int i;
if (the_game == GRAVITY)
{
if (move->column < 0 || move->column >= num_columns)
return -1;
if (conn_board[0][move->column] != 0)
return -1; /*column is full */
i = num_rows - 1;
while (conn_board[i][move->column] != 0)
i--;
conn_board[i][move->column] = player_no;
move->row = i;
}
else
/*game type is NOGRAVITY */
{
if (move->column < 0 || move->column >= num_columns ||
move->row < 0 || move->row >= num_rows)
return -1;
if (conn_board[move->row][move->column] != 0)
return -1; /*spot already taken */
else
conn_board[move->row][move->column] = player_no;
}
num_pieces++;
return 0;
}
/****************************************************************************/
int
check_for_win (coord piece, coord * begin, coord * end)
{
int num;
int player_no;
int row, col;
int i, j, k;
int to_add[4][2] =
{
{0, 1},
{1, 1},
{1, -1},
{1, 0}};
for (k = 0; k < 4; k++)
{
num = 0;
i = to_add[k][0];
j = to_add[k][1];
row = piece.row;
col = piece.column;
player_no = conn_board[row][col];
while (board_piece (row, col) == player_no)
{
num++;
end->row = row;
end->column = col;
row = row + i;
col = col + j;
}
row = piece.row;
col = piece.column;
num--;
while (board_piece (row, col) == player_no)
{
num++;
begin->row = row;
begin->column = col;
row = row - i;
col = col - j;
}
if (num >= num_to_connect)
return 0;
}
return -1;
}
/**************************************************************************/
int
check_for_tie ()
{
if (num_pieces >= (num_rows * num_columns))
return 0;
else
return -1;
}
/*****************************************************************************/
/*internals*******************************************************************/
/*****************************************************************************/
/*
* get player number who has piece at row, column
*/
int
board_piece (int row, int column)
{
if (row < 0 || column < 0 || row >= num_rows || column >= num_columns)
return -1;
else
return conn_board[row][column];
}
/****************************************************************************/